<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>镇长地图</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <!-- 引入 ECharts -->
    <script src="https://cdn.jsdelivr.net/npm/echarts@5.3.2/dist/echarts.min.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        #app {
            width: 100vw;
            height: 100vh;
            font-size: 0.6rem;
            margin: 0;
            padding: 0;
            background-repeat: no-repeat;
            background-position: 0 0 / cover;
            background-color: #101129;
        }
    </style>
</head>

<body>
    <div id="app" v-loading="loading" element-loading-text="加载中" element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)">
        <div id="mainMap" style="height: 100%; width: 100%;"></div>
        <!-- <el-button type="success">成功按钮</el-button> -->

    </div>


    <script>
        new Vue({
            el: '#app', // 指定Vue实例控制的DOM元素
            data: { // 定义数据对象
                message: 'Hello from Vue!',
                bigDataList: [],
                dongtaijson: {},
                loading: false
            },
            mounted() {
                this.getShengJson()
            },
            methods: {
                // 获取省级地图数据
                async getShengJson() {
                    this.loading = true
                    const response = await fetch(`https://geo.datav.aliyun.com/areas_v3/bound/100000_full.json`);
                    if (response.status == 200) {
                        this.dongtaijson = await response.json();
                        // 初始化有数据了,开始渲染地图
                        this.getEcharts();
                        this.loading = false
                        console.log('huoqu', this.dongtaijson);
                    } else {
                        this.loading = false
                    }

                },
                getEcharts() {
                    var that = this

                    const charEle = document.getElementById("mainMap");
                    const charEch = echarts.init(charEle);
                    // 检查地图是否已经注册
                    if (!echarts.getMap("map")) {
                        echarts.registerMap("map", { geoJSON: this.dongtaijson });
                    }

                    const options = {
                        tooltip: {
                            trigger: "item",
                            // formatter: function (params) {
                            //     console.log(params, "vvvvvvvvvvvv");
                            //     return `${params.data.name}: ${params.value || "-"}`;
                            // },
                        },

                        grid: {
                            left: "0px",
                            right: "80px",
                            top: "10px",
                            bottom: "10px",
                        },
                        geo: {
                            map: "map",
                            zoom: 1.1,
                            // 最小缩放
                            scaleLimit: {
                                min: 0.8,
                            },
                            label: {
                                color: "#fff",
                                show: true,
                                formatter: function (params) {
                                    // 自定义标签内容，这里直接返回地名，你也可以添加更多内容或逻辑
                                    return `${params.name}`;
                                },
                            },
                            emphasis: {
                                label: {
                                    color: "#fff",
                                    show: true,
                                },
                                itemStyle: {
                                    areaColor: {
                                        x: 0,
                                        y: 0,
                                        x2: 0,
                                        y2: 1,
                                        colorStops: [
                                            {
                                                offset: 0,
                                                color: "#073684", // 0% 处的颜色
                                            },
                                            {
                                                offset: 1,
                                                color: "#2B91B7", // 100% 处的颜色
                                            },
                                        ],
                                    },
                                },
                            },
                            roam: true,
                            itemStyle: {
                                areaColor: "rgba(0, 0, 0, 0)", // 设置为透明色
                                borderColor: new echarts.graphic.LinearGradient(
                                    0,
                                    0,
                                    0,
                                    1,
                                    [
                                        {
                                            offset: 0,
                                            color: "#00F6FF",
                                        },
                                        {
                                            offset: 1,
                                            color: "#87ADCB",
                                        },
                                    ],
                                    false
                                ),
                                shadowColor: "rgba(10,76,139,1)",
                                shadowOffsetY: 0,
                                shadowBlur: 60,
                                borderWidth: 1,
                            },
                            // regions: [
                            //     {
                            //         name: '山西省', //区块名称
                            //         itemStyle: {
                            //             normal: {
                            //                 areaColor: '#2B91B7'

                            //             }
                            //         }
                            //     },
                            //     {
                            //         name: '江苏省', //区块名称
                            //         itemStyle: {
                            //             normal: {
                            //                 areaColor: '#2B91B7'

                            //             }
                            //         }
                            //     }
                            // ],
                            tooltip: {
                                show: false,
                            },
                            // 添加立体效果
                            viewControl: {
                                distance: 100, // 地图视角的远近，默认为100
                                alpha: 30, // 地图视角绕 x 轴（左右旋转）的角度，默认为 30
                                beta: 0, // 地图视角绕 y 轴（上下旋转）的角度，默认为 0
                            },
                        },
                        series: [
                            {
                                name: "太仓地图",
                                type: "map",
                                map: "map", // 确保与地图的名称对应
                                geoIndex: 0, // 设置对应的 geoIndex
                                data: this.dongtaijson.features.map((item, index) => {
                                    // const townName = item.properties.name;
                                    let itemStyle = {
                                        areaColor: '#FFD700', // 使用映射的颜色
                                    };
                                    return {
                                        name: item.properties.name,
                                        code: item.properties.adcode,
                                        istownMayor: index == 0 ? true : false,
                                        itemStyle
                                    }
                                }),
                            },
                        ],
                    };
                    charEch.setOption(options);
                    charEch.on('click', async function (params) {
                        if (params.componentType === 'series' && params.seriesType === 'map') {
                            this.loading = true
                            const response = await fetch(`https://geo.datav.aliyun.com/areas_v3/bound/${params.data.code}_full.json`);
                            if (response.status == 200) {
                                this.loading = false
                                const data = await response.json();
                                console.log(data, 'fffffffffffffffffffffffffffffffffffffff')
                                charEch.clear()
                                let mapData = data.features.map((item, index) => {
                                    return {
                                        name: item.properties.name,
                                        code: item.properties.adcode,
                                        istownMayor: index == 0 ? true : false
                                    }
                                })
                                charEch.resize()
                                echarts.registerMap("map", { geoJSON: data });
                                that.getEcharts()
                                charEch.setOption({
                                    // tooltip: {
                                    //     trigger: "item",
                                    //     formatter: function (params) {
                                    //         console.log(params, "vvvvvvvvvvvv");
                                    //         return `${params.data.name}: ${params.value || "-"}`;
                                    //     },
                                    // },

                                    grid: {
                                        left: "0px",
                                        right: "80px",
                                        top: "10px",
                                        bottom: "10px",
                                    },
                                    geo: {
                                        map: "map",
                                        zoom: 1.2,
                                        // 最小缩放
                                        scaleLimit: {
                                            min: 0.8,
                                        },
                                        label: {
                                            color: "#fff",
                                            show: true,
                                        },
                                        emphasis: {
                                            label: {
                                                color: "#fff",
                                                show: true,
                                                formatter: function (params) {
                                                    // 自定义标签内容，这里直接返回地名，你也可以添加更多内容或逻辑
                                                    return `${params.name}`;
                                                },
                                            },
                                            itemStyle: {
                                                areaColor: {
                                                    x: 0,
                                                    y: 0,
                                                    x2: 0,
                                                    y2: 1,
                                                    colorStops: [
                                                        {
                                                            offset: 0,
                                                            color: "#073684", // 0% 处的颜色
                                                        },
                                                        {
                                                            offset: 1,
                                                            color: "#2B91B7", // 100% 处的颜色
                                                        },
                                                    ],
                                                },
                                            },
                                        },
                                        roam: true,
                                        itemStyle: {
                                            areaColor: "rgba(0, 0, 0, 0)", // 设置为透明色
                                            borderColor: new echarts.graphic.LinearGradient(
                                                0,
                                                0,
                                                0,
                                                1,
                                                [
                                                    {
                                                        offset: 0,
                                                        color: "#00F6FF",
                                                    },
                                                    {
                                                        offset: 1,
                                                        color: "#87ADCB",
                                                    },
                                                ],
                                                false
                                            ),
                                            shadowColor: "rgba(10,76,139,1)",
                                            shadowOffsetY: 0,
                                            shadowBlur: 60,
                                            borderWidth: 1,
                                        },
                                        tooltip: {
                                            show: false,
                                        },
                                        // 添加立体效果
                                        viewControl: {
                                            distance: 100, // 地图视角的远近，默认为100
                                            alpha: 30, // 地图视角绕 x 轴（左右旋转）的角度，默认为 30
                                            beta: 0, // 地图视角绕 y 轴（上下旋转）的角度，默认为 0
                                        },
                                    },
                                    series: [
                                        {
                                            name: "太仓地图",
                                            type: "map",
                                            map: "map", // 确保与地图的名称对应
                                            geoIndex: 0, // 设置对应的 geoIndex
                                            data: mapData
                                        },
                                    ],
                                }, true);
                            } else {
                                this.loading = false
                                // let baseUrl = 'https://dc.zygyx.com/'
                                let baseUrl = 'https://localhost:5001/'
                                $.get(`${baseUrl}/miniapp/Arera/GetZhen`, {code:`370923000000`  }, function (response) {
                                    // 请求成功时的回调函数
                                    console.log(response);
                                }).fail(function (xhr, status, error) {
                                    // 请求失败时的回调函数
                                    console.error(error);
                                });
                                this.getTownList()
                                console.log('请求失败');
                            }

                            // 在此处处理点击事件，尝试获取code
                        }
                    });
                    return charEch;
                },
                // 获取区县下的所有镇的数据
                getTownList() {
                    // $.ajax({
                    //     type: 'GET',  // 请求的方式，例如 'GET' 或 'POST'
                    //     url: `https://dc.zygyx.com/miniapp/Arera/?code=${370923000000}`,  // 请求的URL地址
                    //     data: {

                    //     },  // 这次请求要携带的数据
                    //     success: function (res) {

                    //     }  // 请求成功之后的回调函数
                    // })


                    $.get(`https://dc.zygyx.com/miniapp/Arera/?code=${370923000000}`, { param1: 'value1', param2: 'value2' }, function (response) {
                        // 请求成功时的回调函数
                        console.log(response);
                    }).fail(function (xhr, status, error) {
                        // 请求失败时的回调函数
                        console.error(error);
                    });

                }
            },
        });
    </script>
</body>

</html>